package com.slwish.xty.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.slwish.common.base.PresenterToolbarActivity;
import com.slwish.common.config.Contants;
import com.slwish.common.interf.PerfectClickListener;
import com.slwish.common.utils.IntentUtils;
import com.slwish.common.utils.SpUtils;
import com.slwish.xty.R;
import com.slwish.xty.adapter.SubscribeListAdapter;
import com.slwish.xty.config.SubscribeConfig;
import com.slwish.xty.entity.SubscribeModel;
import com.slwish.xty.event.SubscribeEvent;
import com.slwish.xty.mvp.contract.SubscribeListContract;
import com.slwish.xty.mvp.presenter.SubscribeListPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 预约列表
 */
public class SubscribeListActivity extends PresenterToolbarActivity<SubscribeListContract.Presenter> implements SubscribeListContract.View, XRecyclerView.LoadingListener {
    @BindView(R.id.xrecyclerview)
    XRecyclerView xrecyclerview;
    SubscribeListAdapter mAdapter;
    List<SubscribeModel> mLists = new ArrayList<>();
    private long userId;

    @Override
    protected int getLayoutId() {
        return R.layout.act_subscribe_list;
    }

    /**
     *
     */
    @Override
    protected void initWidget() {
        super.initWidget();
        setTitle(R.string.my_yy_title);
        userId = SpUtils.getLong(mContext, Contants.UserCode, 0);
        xrecyclerview.setLayoutManager();
        xrecyclerview.setLoadingListener(this);
        mAdapter = new SubscribeListAdapter(mLists);
        xrecyclerview.setAdapter(mAdapter);
        xrecyclerview.refresh();
    }

    @Override
    protected boolean isEventBus() {
        return true;
    }

    @Override
    protected void initData() {
        super.initData();
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
        EventBus.getDefault().post("closeDrawer");
//            }
//        },1000);
    }

    @Override
    protected void initEvent() {
        super.initEvent();

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                SubscribeModel item = mAdapter.getItem(position - 1);
                if (view.getId() == R.id.tv_qd) {
                    if (TextUtils.equals(item.getStatus(), "2") && TextUtils.equals(item.getSignIn(), "0")) {
                        //到签到页面
                        Bundle bundle = new Bundle();
                        bundle.putInt(Contants.ID, item.getOrderId());
                        bundle.putInt("POSITION", position - 1);
                        IntentUtils.startActivity(mContext, SignActivity.class, bundle);
                    } else {
                        //再次预约
                        Bundle bundle = new Bundle();
                        bundle.putInt(Contants.ID, item.getMid());
                        IntentUtils.startActivity(mContext, SubscribeEditActivity.class, bundle);
                    }
                }
            }
        });

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SubscribeModel item = mAdapter.getItem(position - 1);

                boolean isShowBottom = SubscribeConfig.isShow(item);
                Bundle bundle = new Bundle();
                bundle.putSerializable(Contants.MODEL, item);
                bundle.putBoolean("ISSHOWBOTTOM", isShowBottom);
                bundle.putInt("POSITION", position - 1);
                IntentUtils.startActivity(mContext, SubscribeInfoActivity.class, bundle);
            }
        });
    }

    /**
     * 查询成功返回
     *
     * @param model
     */
    @Override
    public void querySubscribeSuccess(List<SubscribeModel> model) {
        if (model != null && model.size() > 0) {
            if (mPagerCofig.getCurrentPage() == 1) {
                mLists.clear();
                xrecyclerview.refreshComplete();
            }
            mLists.addAll(model);
            xrecyclerview.setNoMore(false);
            if (mPagerCofig.getCurrentPage() != 1)
                mAdapter.notifyItemRangeChanged(mLists.size() - model.size(), mLists.size() - 1);
        } else {
            if (mPagerCofig.getCurrentPage() == 1) {
                //显示无数据界面
                View view = View.inflate(mContext, R.layout.layout_empty, null);
                TextView tvNoData = view.findViewById(R.id.tv_no_data);
                TextView tvReset = view.findViewById(R.id.tv_reset);
                tvReset.setVisibility(View.VISIBLE);
                tvNoData.setText(R.string.no_credit_data_text);
                tvReset.setOnClickListener(new PerfectClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        IntentUtils.startActivity(mContext, SubscribeEditActivity.class);
                    }
                });
                mAdapter.setEmptyView(view);
                xrecyclerview.refreshComplete();
            }
            xrecyclerview.setNoMore(true);
        }
    }

    @Override
    public void querySubscribeFailed() {
        if (mLists == null || mLists.size() < 1) {
            View view = View.inflate(mContext, R.layout.layout_timeout, null);
            mAdapter.setEmptyView(view);
            xrecyclerview.refreshComplete();
            xrecyclerview.setNoMore(true);
            view.findViewById(R.id.tv_reset_loading).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    xrecyclerview.refresh();
                }
            });
        }
    }

    @Override
    protected SubscribeListContract.Presenter initPresenter() {
        return new SubscribeListPresenter(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateAdapter(SubscribeEvent messageEvent) {
        try {
            int position = messageEvent.getPosition();
            mLists.get(position).setStatus("6").setSignIn("1");
            mAdapter.notifyItemChanged(position);
        } catch (Exception E) {
        }
    }

    /**
     * 下拉
     */
    @Override
    public void onRefresh() {
        mPagerCofig.setCurrentPage(1);
        mPresenter.querySubscribeByUserId(userId, mPagerCofig.getCurrentPage());
    }

    /**
     * 上拉
     */
    @Override
    public void onLoadMore() {
        Log.i("xx", "onLoadMore");
        if (mPagerCofig.getCurrentPage() == mAdapter.getItemCount() / 10) {
            mPagerCofig.setCurrentPage(mPagerCofig.getCurrentPage() + 1);
            mPresenter.querySubscribeByUserId(userId, mPagerCofig.getCurrentPage());
        } else xrecyclerview.setNoMore(true);
    }
}
